﻿using System.ComponentModel.DataAnnotations;

namespace IssueVision.EntityModel
{
    public static class IssueRules
    {
        /// <summary>
        /// When status is Resolved, the ResolutionID cannot be null or zero,
        /// and when the status is not Resolved, the ResolutionID should be
        /// either null or zero.
        /// </summary>
        /// <param name="issue"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckStatusResolved(Issue issue, ValidationContext validationContext)
        {
            if (issue.StatusID == IssueVisionServiceConstant.ResolvedStatusID &&
                (issue.ResolutionID == null || issue.ResolutionID == 0))
                return new ValidationResult("Resolution is required when the status is Resolved.", new[] { "StatusID", "ResolutionID" });

            if (issue.StatusID != IssueVisionServiceConstant.ResolvedStatusID &&
                issue.ResolutionID != null && issue.ResolutionID != 0)
                return new ValidationResult("Resolution is not needed when the status is not Resolved.", new[] { "StatusID", "ResolutionID" });

            return ValidationResult.Success;
        }

        /// <summary>
        /// When status is Active, assigned-to-user cannot be null.
        /// </summary>
        /// <param name="issue"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckStatusActive(Issue issue, ValidationContext validationContext)
        {
            if (issue.StatusID == IssueVisionServiceConstant.ActiveStatusID && issue.AssignedToID == null)
                return new ValidationResult("A user is required when the status is Active. ", new[] { "StatusID", "AssignedToID" });

            return ValidationResult.Success;
        }

        /// <summary>
        /// When status is Open, assigned-to-user should be null.
        /// </summary>
        /// <param name="issue"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckStatusOpen(Issue issue, ValidationContext validationContext)
        {
            if (issue.StatusID == IssueVisionServiceConstant.OpenStatusID && issue.AssignedToID != null)
                return new ValidationResult("AssignedTo user is not needed when the status is Open.", new[] { "StatusID", "AssignedToID" });

            return ValidationResult.Success;
        }
    }
}